import {
    request
} from '@/common/service/request.js'
import {
    msg,
    close,
    loading,
    hide,
    confirm,
    alert
} from '@/common/utils/dialog.js'
/**
 * 订单处理-统一入口
 */
const actionHandle = (event) => {
    console.log(event)
    return new Promise((resolve, reject) => {
        switch (event.type) {
            case 'detail':
                uni.navigateTo({
                    url: `/pages/order/detail/detail?orderId=${event.data.OrderId}`
                });
                break;
            case 'cancel':
                confirm('提示', '确定要取消定购？').then(res => {
                    if (res) {
                        request('order', 'cancel', {
                            OrderId: event.data.OrderId
                        }).then(delRes => {
                            if (delRes.code == 1) {
                                msg('取消成功');
                                uni.$emit('order-update', {
                                    type: 'update',
                                    orderId: event.data.OrderId
                                })
                            } else {
                                msg(delRes.msg);
                            }
                        }).catch((err) => {
                            msg(err.msg);
                        })
                    }
                })
                break;
            case 'delete':
                confirm('提示', '确定要删除定购记录？').then(res => {
                    if (res) {
                        loading('正在删除');
                        request('order', 'delete', {
                            OrderId: event.data.OrderId
                        }).then((delRes) => {
                            if (delRes.code == 1) {
                                msg('删除成功');
                                uni.$emit('order-update', {
                                    type: 'delete',
                                    orderId: event.data.OrderId
                                })
                                uni.navigateBack();
                            } else {
                                msg(delRes.msg);
                            }
                        }).catch(err => {
                            msg(err.msg);
                        });
                    }
                });
                break;
            case 'receive':
                confirm('提示', '确认服务已经完成？').then(res => {
                    if (res) {
                        loading('处理中');
                        request('order', 'confirm', {
                            OrderNumber: event.data.OrderNo
                        }).then((res) => {
							if (res.code == 1) {
								uni.reLaunch({
									url:`/pages/order/index?orderMessage=receive`
								})
							} else {
								this.$dialog.msg(res.msg);
							}
						  
						})
                    }
                });
                break;
            case 'phone':
                uni.makePhoneCall({
                    phoneNumber: event.data.ShopMobile
                });
                break;
            default:
                break;
        }
    })
}

export default {
    actionHandle,
}
